<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>setsockopt</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4>NAME</h4><blockquote>
setsockopt - set the socket options
</blockquote><h4>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="syssocket.h.html">sys/socket.h</a>&gt;

int setsockopt(int <I>socket</I>, int <I>level</I>, int <I>option_name</I>, const void
                *<I>option_value</I>, socklen_t <I>option_len</I>);
</code>
</pre>
</blockquote><h4>DESCRIPTION</h4><blockquote>
The
<i>setsockopt()</i>
function sets the option specified by the <I>option_name</I> argument, at the
protocol level specified by the <I>level</I> argument, to the value pointed to
by the <I>option_value</I> argument for the socket associated with the file
descriptor specified by the <I>socket</I> argument.
<p>
The <I>level</I> argument
specifies the protocol level at which the option resides.  To set options at
the socket level, specify the <I>level</I> argument as SOL_SOCKET.  To set
options at other levels, supply the appropriate protocol number for the
protocol controlling the option.  For example, to indicate that an option will
be interpreted by the TCP (Transport Control Protocol), set <I>level</I> to the
protocol number of TCP, as defined in the &lt;<B>netinet/in.h</B>&gt; header, or as
determined by using
<i><a href="getprotobyname.html">getprotobyname()</a></i>.
<p>
The <I>option_name</I> argument
specifies a single option to set.  The <I>option_name</I> argument and any
specified options are passed uninterpreted to the appropriate protocol module
for interpretations.  The &lt;<B>sys/socket.h</B>&gt; header defines the socket level
options.  The options
are as follows:
<dl compact>

<dt>SO_DEBUG<dd>Turns on recording of debugging information.  This option enables or disables
debugging in the underlying protocol modules.  This option takes an
<B>int</B> value.
This is a boolean option.

<dt>SO_BROADCAST<dd>Permits sending of broadcast messages, if this is supported by the protocol.
This option takes an <B>int</B> value.
This is a boolean option.

<dt>SO_REUSEADDR<dd>Specifies that the rules used in validating addresses supplied to
<i><a href="bind.html">bind()</a></i>
should allow reuse of local addresses, if this is supported by the protocol.
This option takes an <B>int</B> value.
This is a boolean option.

<dt>SO_KEEPALIVE<dd>Keeps connections active by enabling the periodic transmission of messages, if
this is supported by the protocol.  This option takes an <B>int</B> value.

If the connected socket fails to respond to these messages, the connection is
broken and processes writing to that socket are notified with a SIGPIPE
signal.

This is a boolean option.

<dt>SO_LINGER<dd>Lingers on a
<i><a href="close.html">close()</a></i>
if data is present.  This option controls the action taken when unsent
messages queue on a socket and
<i><a href="close.html">close()</a></i>
is performed.  If SO_LINGER is set, the system blocks the process during
<i><a href="close.html">close()</a></i>
until it can transmit the data or until the time expires.  If SO_LINGER is not
specified, and
<i><a href="close.html">close()</a></i>
is issued, the system handles the call in a way that allows the process to
continue as quickly as possible.  This option takes a <B>linger</B> structure,
as defined in the &lt;<B>sys/socket.h</B>&gt; header, to specify the state of
the option and linger interval.

<dt>SO_OOBINLINE<dd>Leaves received out-of-band data (data marked urgent) in line.
This option takes an <B>int</B> value.
This is a boolean option.

<dt>SO_SNDBUF<dd>Sets send buffer size. 
This option takes an 
<B>int</B>
value.

<dt>SO_RCVBUF<dd>Sets receive buffer size.  This option takes an 
<B>int</B>
value.

<dt>SO_DONTROUTE<dd>Requests that outgoing messages bypass
the standard routing facilities.
The destination must be on a directly-connected 
network, and messages are directed 
to the appropriate network interface according
to the destination address. The effect, if any,
of this option depends on what protocol is
in use. 
This option takes an 
<B>int</B>
value.
This is a boolean option.

<dt>SO_RCVLOWAT<dd>Sets the minimum number of bytes to process 
for socket input operations.  The default value
for SO_RCVLOWAT is 1.  If SO_RCVLOWAT is set to 
a larger value, blocking receive calls normally 
wait until they have received the smaller of the
low water mark value or the requested amount. 
(They may return less than the low water mark 
if an error occurs, a signal is caught, or the 
type of data next in the receive queue is
different than that returned, e.g. out of band 
data). 
This option takes an 
<B>int</B>
value.
Note that not all implementations allow this option to be set.

<dt>SO_RCVTIMEO<dd>Sets the timeout value that specifies the maximum amount
of time an input function waits until it completes.
It accepts a 
<B>timeval</B>
structure with the number of seconds and microseconds specifying the limit 
on how long to wait for an input operation
to complete.  If a receive operation has blocked
for this much time without receiving additional
data, it returns with a partial count or
<I>errno</I>
set to [EAGAIN] or [EWOULDBLOCK] if no 
data were received.  The default for this
option 
is zero, which indicates that a receive
operation
will not time out.
This option takes a 
<B>timeval</B>
structure.
Note that not all implementations allow this option to be set.

<dt>SO_SNDLOWAT<dd>Sets the minimum number of bytes to process for
socket output operations. Non-blocking output
operations  will process no data if flow control
does not allow the smaller of the send low
water mark value or the entire request to be processed.
This option takes an 
<B>int</B>
value.
Note that not all implementations allow this option to be set.

<dt>SO_SNDTIMEO<dd>Sets the timeout value specifying the amount of time 
that an output function blocks because flow 
control prevents data from being sent.
If a send operation has blocked for this 
time, it returns with a partial count or
with errno set to [EAGAIN] ore [EWOULDBLOCK] if 
no data were sent.  The default for this option 
is zero, which indicates that a send operation
will not time out.
This option stores a 
<B>timeval</B>
structure.
Note that not all implementations allow this option to be set.

</dl>
<p>
For boolean options, 0 indicates that the option is disabled and 1 indicates
that the option is enabled.
<p>
Options at other protocol levels vary in format and name.
</blockquote><h4>RETURN VALUE</h4><blockquote>
Upon successful completion,
<i>setsockopt()</i>
returns 0.  Otherwise, -1 is returned and <I>errno</I>
is set to indicate the error.
</blockquote><h4>ERRORS</h4><blockquote>
The
<i>setsockopt()</i>
function will fail if:
<dl compact>

<dt>[EBADF]<dd>The <I>socket</I> argument is not a valid file descriptor.

<dt>[EDOM]<dd>The send and receive timeout values are too
big to fit into the timeout fields in the socket structure.

<dt>[EFAULT]<dd>The 
<I>option_value</I>
parameter can not be accessed or written.

<dt>[EINVAL]<dd>The specified option is invalid at the specified socket level or the socket
has been shut down.

<dt>[EISCONN]<dd>The socket is already connected, and a 
specified option can not be set while the socket is connected.

<dt>[ENOPROTOOPT]<dd>The option is not supported by the protocol.

<dt>[ENOTSOCK]<dd>The <I>socket</I> argument does not refer to a socket.

</dl>
<p>
The
<i>setsockopt()</i>
function may fail if:
<dl compact>

<dt>[ENOMEM]<dd>There was insufficient memory available for the operation to complete.

<dt>[ENOBUFS]<dd>Insufficient resources are available in the system to complete the call.

<dt>[ENOSR]<dd>There were insufficient STREAMS resources available for the operation to
complete.

</dl>
</blockquote><h4>APPLICATION USAGE</h4><blockquote>
The
<i>setsockopt()</i>
function provides an application program with the means to control socket
behaviour.  An application program can use
<i>setsockopt()</i>
to allocate buffer space, control timeouts, or permit socket data
broadcasts.  The
<i><a href="syssocket.h.html">&lt;sys/socket.h&gt;</a></i>
header defines the socket-level options available to
<i>setsockopt()</i>.
<p>
Options may exist at multiple protocol levels.
The SO_ options are always present at the uppermost socket level.
</blockquote><h4>SEE ALSO</h4><blockquote>
<i><a href="bind.html">bind()</a></i>,
<i><a href="endprotoent.html">endprotoent()</a></i>,
<i><a href="getsockopt.html">getsockopt()</a></i>,
<i><a href="socket.html">socket()</a></i>,
<i><a href="syssocket.h.html">&lt;sys/socket.h&gt;</a></i>.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
